package com.itheima.duotai3;

import javax.xml.crypto.dom.DOMCryptoContext;

public class Test {
    public static void main(String[] args) {
/*        Dog dog = new Dog();
        useAnimal(dog);
        Cat cat = new Cat();
        useAnimal(cat);*/
//返回值用父类 调用者 灵活了
        Animal animal = getAnimal();
        animal.eat();
        //ClassCastException
        if(animal instanceof Dog){
            Dog dog= (Dog) animal;
            dog.kanjia();
        }



    }
    //定义一个方法  参数  直接来 父类类型  方法内部灵活了
    public static void  useAnimal(Animal a){
        a.eat();
    }
    public static Animal getAnimal(){
        return new Dog();
        //return new Cat();
    }
}

abstract class Animal {
    public void drink() {
        System.out.println("喝水");
    }
    public abstract void eat();
}
class Dog extends Animal{

    @Override
    public void eat() {
        System.out.println("狗吃肉");
    }

    public void kanjia(){
        System.out.println("狗看家");
    }
}
class Cat extends Animal{

    @Override
    public void eat() {
        System.out.println("猫吃鱼");
    }
}